

#include "currencyChassis_savePoseByOdom_sql.h"
/**
 * 1.小车位置的数据给server,server计算二维码在基坐标的位置并保存
 * 2.小车位置的数据给server,server计算小车在基坐标的位置并保存
 * qt操作界面
 * sukai
 * 2021-10-15
 */
SavePoseByOdom::SavePoseByOdom(QWidget *parent):QDialog(parent, Qt::WindowCloseButtonHint | Qt::WindowMinMaxButtonsHint | Qt::WindowStaysOnTopHint) {
    //初始化界面
    initUI();
    //按钮点击事件
    bindSignal();
    recv_prefix_green = "<font color='green'> &nbsp; &nbsp; &nbsp; &nbsp; "; // 空格：“&nbsp;”
    recv_prefix_red = "<font color='red'> &nbsp; &nbsp; &nbsp; &nbsp; "; // 空格：“&nbsp;”
}
void SavePoseByOdom::initUI() {
    //窗体设置
    setWindowTitle("当前位置标记marker点位_TCP_GUI");
    setFixedSize(830, 499);
    //布局
    QHBoxLayout  *Hlayout = new QHBoxLayout;
    QVBoxLayout  *Vlayout = new QVBoxLayout;
    //二维码id输入框
    markerIdEdit = new QLineEdit("-1");
    //桌子id号
    foodTableIdEdit=new QLineEdit("-1");
    //房间名字
    areaIdEdit=new QLineEdit("");
    //标点类型
    typeIdEdit=new QLineEdit("0");
    //标点编码
    markNumEdit=new QLineEdit("1");

    //点位到达后需要向外发送的tcp指令
    commandEdit=new QLineEdit("EMPTY");
    //是否启用 位到达后向外发送的tcp指令 -1：禁止 ； 1：启用
    commandStatus=new QComboBox(this);
//    robotfloor=new QComboBox(this);

    commandStatus->addItem("不启用");
    commandStatus->addItem("启用");
//    //路点所在的当前楼层
//    robotfloor =new QComboBox(this);
//    robotfloor->addItem("请选择楼层");
//    for (int i = 1; i <=5 ; ++i) {
//        QString floor=QString::number(i);
//        robotfloor->addItem(floor);
//    }
    layout.addRow("点位号: ", foodTableIdEdit);
    layout.addRow("点位名字: ", areaIdEdit);
  //  layout.addRow("房间所在的楼层: ", robotfloor);
  //  layout.addRow("二维码id: ", markerIdEdit);
    layout.addRow("点位类型: ", typeIdEdit);
    layout.addRow("点位编码: ", markNumEdit);
    layout.addRow("到达点位后发送的tcp命令: ", commandEdit);
    layout.addRow("是否启用到达点位后发送tcp命令: ", commandStatus);

    // 创建两个按钮
    markerBtn = new QPushButton("计算二维码pose");
    carBtn = new QPushButton("计算小车的pose");
    Vlayout->addLayout(&layout);
    savefoodTablePoseBtn.setText("保存小车在房间的目标点的位置");
    //更新小车在桌子目标点的位置
    updatefoodTablePoseBtn.setText("更新小车在房间的目标点的位置");
    initialPoseBtn.setText("计算小车的pose并initialPose更新小车姿态");
    //back_Btn.setText("返回ui");
    //清除日志
    clear_logBtn.setText("清除日志");

    //计算二维码pose和计算小车的pose
  //  Hlayout->addWidget(markerBtn);
  //  Hlayout->addWidget(carBtn);
  //  Vlayout->addLayout( Hlayout);
  //  Vlayout->addWidget(&initialPoseBtn);
    Vlayout->addWidget(&savefoodTablePoseBtn);
    //Vlayout->addWidget(&updatefoodTablePoseBtn);
   // Vlayout->addWidget(&back_Btn);
    Vlayout->addWidget(&clear_logBtn);
    Vlayout->addWidget(&logTextEdit);
    //设置布局
    setLayout(Vlayout);

}



void SavePoseByOdom::bindSignal() {

    // 计算二维码pose
    connect(markerBtn,&QPushButton::clicked,this,&SavePoseByOdom::computerMarkerPose);
    // 计算小车的pose
    connect(carBtn,&QPushButton::clicked,this,&SavePoseByOdom::computerCarPose);
    //保存小车在桌子目标点的位置
    connect(&savefoodTablePoseBtn, &QPushButton::clicked, this, &SavePoseByOdom::saveFoodCarPose);
    //更新小车在桌子目标点的位置
    connect(&updatefoodTablePoseBtn, &QPushButton::clicked, this, &SavePoseByOdom::updateFoodCarPose);
    //计算小车的pose并initialPose更新小车姿态
    connect(&initialPoseBtn, &QPushButton::clicked, this, &SavePoseByOdom::computerInitialPose);
    //返回ui页面
    connect(&back_Btn, &QPushButton::clicked, this, &SavePoseByOdom::back);
    //清除日志
    connect(&clear_logBtn, &QPushButton::clicked, this, &SavePoseByOdom::clear_log);

}




// 计算二维码pose
void SavePoseByOdom::computerMarkerPose(){
   // computerMarkerPoseCallBack();
}
// 计算小车的pose
void SavePoseByOdom::computerCarPose(){
 //   computerCarPoseCallBack();
}

//计算小车的pose 并更新小车pose
void SavePoseByOdom::computerInitialPose(){
 //   computerInitialPoseCallBack();
}

//保存小车在桌子旁的pose回调函数
void SavePoseByOdom::saveFoodCarPose(){
 //   foodCarPoseCallBack();
    //  /api/markers/insert?name=charge_dock_2&type=11&num=33393&command=command&commandStatus=commandStatus
   if(getArea().size()>0){
       QString msg="/api/markers/insert?name=";
        msg=msg+getArea()+"&type="+getTypeId()+"&num="+getMarkNum()+"&command="+getCommand()+"&commandStatus="+getCommandStatus();
       sendMsg(msg);

       setFoodTableIdAndArea();
        show_console("保存成功！"+msg,"get");
   }else{
        show_console("保存失败请输入点位名称！","send");
   }

}
//sukai 发送数据
void SavePoseByOdom::sendMsg(QString msg)
{
  IIMTDriver::getInstance()->sendMsg(msg);

}
//更新小车在桌子旁的pose回调函数
void SavePoseByOdom::updateFoodCarPose(){
  //  foodCarPoseCallBackUpdate();
}
/**
 * 显示日志
 */

void SavePoseByOdom::show_console(QString msg_log,QString send) {
    char response[320];
    time_t tt;
    char buf[16] = {0};
    char buf1[16] = {0};
    tt = time(NULL);
    //清除char缓存
    memset(response, 0, 320);
    strftime(buf, 16, "%y-%m-%d %H:%M:%S", localtime(&tt));
    QString qss="[本地时间: "+QString(buf)+" ]";
    QByteArray ba = qss.toLocal8Bit();
    memcpy(response, ba.data(), ba.size());

    if (send == "send") {//发送消息
      logTextEdit.append(recv_prefix_red + response + msg_log);

    } else if (send == "get") {//接收到消息
        logTextEdit.append(recv_prefix_green + QString(response) + msg_log);

    }

//     QString msg1 = QString(buf);
//      msg1 = "[本地时间: "+msg1+"] "+msg_log;
//      QByteArray array = msg1.toLocal8Bit();
//       memcpy(response, array.data(), array.size());
//       strftime(buf1, 16, "%y_%m_%d", localtime(&tt));
//       QString logfileTimeName = QString(buf1);

      // Log_Save(response,logfileTimeName);

}
//计算二维码pose回调函数
void SavePoseByOdom::setComputerMarkerPoseCallBack(function<void()> computerMarkerPoseCallBack){
    this->computerMarkerPoseCallBack=computerMarkerPoseCallBack;
}
//计算小车的pose回调函数
void SavePoseByOdom::setComputerCarPoseCallBack(function<void()> computerCarPoseCallBack){
    this->computerCarPoseCallBack=computerCarPoseCallBack;
}

//计算小车的pose 并更新小车pose回调函数
void SavePoseByOdom::setComputerInitialPoseCallBack(function<void()> computerInitialPoseCallBack){
    this->computerInitialPoseCallBack=computerInitialPoseCallBack;

}
//保存小车在桌子旁的pose回调函数
void SavePoseByOdom::setFoodCarPoseCallBack(function<void()> foodCarPoseCallBack){
    this->foodCarPoseCallBack=foodCarPoseCallBack;

}
//更新小车在桌子旁的pose回调函数
void SavePoseByOdom::setFoodCarPoseCallBackUpdate(function<void()> foodCarPoseCallBackUpdate){
    this->foodCarPoseCallBackUpdate=foodCarPoseCallBackUpdate;

}
/**
 * 清除日志
 */
void SavePoseByOdom::clear_log() {

    logTextEdit.clear();
}

double SavePoseByOdom::getMarkerId(){
    markerId=markerIdEdit->text().toDouble();
    return markerId;
}
int SavePoseByOdom::getFoodTableId(){
    foodTableId=foodTableIdEdit->text().toInt();
    return foodTableId;
}
//返回页面
void SavePoseByOdom::back() {

    system("rosnode kill currencyChassis_savePoseByOdom_node");
    sleep(1);
    execlp("rosrun", "rosrun", "ui","ui", NULL);
 //   system("killall -9 visualorientation/listenerCarBaselinkPose_node");
 //   system("killall -9 visualorientation/QRcodeOrlentationCalibration_node");
  //  system("killall -9 visualorientation/carInEyeVerify_node");
  //  system("killall -9 visualorientation/sendOdomClient_sql_node");

}
//targetName
QString SavePoseByOdom::getArea(){
    QString  area=areaIdEdit->text();
    return area;
}
//路点所在的当前楼层
QString SavePoseByOdom::getRobotfloor(){
    QString  area=areaIdEdit->text();
    int robotfloorIndex = robotfloor->currentIndex();
    //   qDebug()<<"editFirst->currentIndex:" <<editFirstIndex;
   // QString robotfloorMsg = robotfloor->currentText();
    return robotfloor->currentText();
}
void SavePoseByOdom::setFoodTableIdAndArea(){
    foodTableIdEdit->setText("-1");
   // areaIdEdit->setText("");
    typeIdEdit->setText("0");
    markNumEdit->setText("1");
    commandEdit->setText("EMPTY");
    commandStatus->setCurrentIndex(0);
}


//标点类型
QString SavePoseByOdom::getTypeId(){
    QString  typeId=typeIdEdit->text();
    return typeId;
}

//标点编码
QString SavePoseByOdom::getMarkNum(){
    QString  markNum=markNumEdit->text();
    return markNum;
}

//点位到达后需要向外发送的tcp指令
QString SavePoseByOdom::getCommand(){
    QString  command=commandEdit->text();
    return command;
}
//是否启用 位到达后向外发送的tcp指令 -1：禁止 ； 1：启用
QString SavePoseByOdom::getCommandStatus(){
    QString flg="-1";
    int robotfloorIndex = commandStatus->currentIndex();
    if(robotfloorIndex==0){
        flg="-1";//禁止
    }else{
        flg="1";//启用
    }
    //   qDebug()<<"editFirst->currentIndex:" <<editFirstIndex;
    // QString robotfloorMsg = robotfloor->currentText();
    return flg;
}
SavePoseByOdom::~SavePoseByOdom() {

}
